/*
 * Copyright 2016 Michael Bely
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.michaelbel.bottomsheet;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.Point;
import ohos.app.Context;
import org.michaelbel.bottomsheet.util.ResTUtil;
import org.michaelbel.bottomsheet.util.WindowUtil;

/**
 * Date: 17 FEB 2018
 * Time: 00:30 MSK
 *
 * @author Michael Bel
 */
@SuppressWarnings("all")
public class BottomSheetCell extends StackLayout implements Component.DrawTask {
    private Paint paint;
    private int cellHeight;
    private boolean divider;

    private Text textView;
    private Image iconView;

    private boolean isSetHeight;

    public BottomSheetCell(Context context) {
        super(context);

        if (paint == null) {
            paint = new Paint();
            paint.setStrokeWidth(1);
            paint.setColor(Color.BLACK);
        }

        cellHeight = AttrHelper.vp2px(48, context);

        iconView = new Image(context);
        iconView.setScaleMode(Image.ScaleMode.CENTER);

        StackLayout.LayoutConfig layoutConfig =
                new StackLayout.LayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT);
        layoutConfig.alignment = LayoutAlignment.VERTICAL_CENTER;
        layoutConfig.setMarginLeft(AttrHelper.vp2px(16, context));
        layoutConfig.setMarginRight(AttrHelper.vp2px(16, context));
        iconView.setLayoutConfig(layoutConfig);
        addComponent(iconView);

        textView = new Text(context);
        textView.setMultipleLine(false);
        textView.setTextSize(16);

        StackLayout.LayoutConfig textConfig =
                new StackLayout.LayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT);
        textConfig.alignment = LayoutAlignment.VERTICAL_CENTER;
        textConfig.setMarginLeft(AttrHelper.vp2px(16, context));
        textConfig.setMarginRight(AttrHelper.vp2px(16, context));
        textView.setLayoutConfig(textConfig);
        addComponent(textView);
    }

    /**
     * VectorIcon 类型
     *
     * @param resVectorId resVectorId
     * @return 无返回值
     */
    public BottomSheetCell setVectorIcon(Element resVectorId) {
        if (resVectorId != null) {
            iconView.setImageElement(resVectorId);

            StackLayout.LayoutConfig textConfig =
                    new StackLayout.LayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT);
            textConfig.alignment = LayoutAlignment.VERTICAL_CENTER;
            textConfig.setMarginLeft(AttrHelper.vp2px(72, mContext));
            textConfig.setMarginRight(AttrHelper.vp2px(16, mContext));
            textView.setLayoutConfig(textConfig);

        } else {
            StackLayout.LayoutConfig textConfig =
                    new StackLayout.LayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT);
            textConfig.alignment = LayoutAlignment.VERTICAL_CENTER;
            textConfig.setMarginLeft(AttrHelper.vp2px(16, mContext));
            textConfig.setMarginRight(AttrHelper.vp2px(16, mContext));
            textView.setLayoutConfig(textConfig);
        }

        return this;
    }

    /**
     * 设置icon
     * @param resId resId
     * @return BottomSheetCell
     */
    public BottomSheetCell setIcon(Element resId) {
        if (resId != null) {
            iconView.setImageElement(resId);

            StackLayout.LayoutConfig textConfig =
                    new StackLayout.LayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT);
            textConfig.alignment = LayoutAlignment.VERTICAL_CENTER;
            textConfig.setMarginLeft(AttrHelper.vp2px(72, mContext));
            textConfig.setMarginRight(AttrHelper.vp2px(16, mContext));
            textView.setLayoutConfig(textConfig);

        } else {
            StackLayout.LayoutConfig textConfig =
                    new StackLayout.LayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT);
            textConfig.alignment = LayoutAlignment.VERTICAL_CENTER;
            textConfig.setMarginLeft(AttrHelper.vp2px(16, mContext));
            textConfig.setMarginRight(AttrHelper.vp2px(16, mContext));
            textView.setLayoutConfig(textConfig);
        }

        return this;
    }

    /**
     * 设置setText
     * @param text text
     * @param color color
     * @return BottomSheetCell
     */
    public BottomSheetCell setText(String text, int color) {
        textView.setText(text);
        textView.setTextColor(new Color(ResTUtil.getColor(mContext, color)));
        return this;
    }

    /**
     * 设置
     * @param height height
     * @return setItemHeight
     */
    public BottomSheetCell setItemHeight(int height) {
        cellHeight = height;
        return this;
    }

    /**
     * 设置divder
     * @param divider divider
     * @return BottomSheetCell
     */
    public BottomSheetCell setDivider(boolean divider) {
        this.divider = divider;
        addDrawTask(this);

        return this;
    }

    /**
     * setDividerColor
     * @param theme theme
     * @return BottomSheetCell
     */
    public BottomSheetCell setDividerColor(boolean theme) {
        paint.setColor(!theme ? Color.BLACK : Color.WHITE);
        addDrawTask(this);
        return this;
    }


    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (divider) {
            setComponentSize(cellHeight + 1, WindowUtil.getWindowWdith(mContext));
            Point pointX = new Point(getPaddingLeft(), getHeight() - 1);
            Point pointY = new Point(getWidth() - getPaddingRight(), getHeight() - 1);

            canvas.drawLine(pointX, pointY, paint);
        } else {
            setComponentSize(cellHeight, WindowUtil.getWindowWdith(mContext));
        }
    }
}
